/**
 * 617. 合并二叉树
 * https://leetcode-cn.com/problems/merge-two-binary-trees/
 */
const { createTreeNode, displayTreeNode } = require('../../libs/public');

/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
function mergeTrees(root1, root2) {
  if (!root1) {
    return root2;
  }
  if (!root2) {
    return root1;
  }
  root1.val += root2.val;
  root1.left = mergeTrees(root1.left, root2.left);
  root1.right = mergeTrees(root1.right, root2.right);
  return root1;
}

console.log(displayTreeNode(mergeTrees(
  createTreeNode([1, 3, 2, 5]),
  createTreeNode([2, 1, 3, null, 4, null, 7]),
)) === '[3,4,5,5,4,null,7]');
console.log(displayTreeNode(mergeTrees(
  createTreeNode([1]),
  createTreeNode([1, 2]),
)) === '[2,2]');
